package com.keon.jianbao.adapter;

import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.keon.jianbao.AppContext;
import com.keon.jianbao.AppManager;
import com.keon.jianbao.R;
import com.keon.jianbao.base.BaseActivity;
import com.keon.jianbao.base.BaseGridViewAdapter;
import com.keon.jianbao.bean.Expert;
import com.keon.jianbao.enu.WebType;
import com.keon.jianbao.ui.activity.UploadJianBaoActivity;
import com.keon.jianbao.ui.activity.WebActivity;
import com.keon.jianbao.util.TDevice;
import com.keon.jianbao.util.UILUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by keon on 2015/9/21.
 */
public class ExpertChooseAdapter extends BaseGridViewAdapter {

    static class ViewHolder {
        @Bind(R.id.expert_choose_img)
        ImageView sdvImg;
        @Bind(R.id.expert_choose_name)
        TextView tvName;
        @Bind(R.id.expert_choose_title)
        TextView tvTitle;
        @Bind(R.id.expert_choose_catalog)
        TextView tvCatalog;
        @Bind(R.id.expert_choose_good)
        TextView tvGood;
        @Bind(R.id.expert_choose_times)
        TextView tvTimes;
        @Bind(R.id.expert_choose_fee)
        TextView tvFee;
        @Bind(R.id.expert_choose_submit)
        TextView tvSubmit;
        @Bind(R.id.expert_choose_top)
        LinearLayout llTop;

        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    @Override
    protected View getRealView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if (convertView == null || convertView.getTag() == null) {
            convertView = getLayoutInflater(parent.getContext()).inflate(
                    R.layout.list_cell_expert_choose, null);
            vh = new ViewHolder(convertView);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }

        final Expert expert = (Expert) mDatas.get(position);

//        Uri uri = Uri.parse(expert.getExpert_face());
//        vh.sdvImg.setImageURI(uri);
        ImageLoader.getInstance().displayImage(expert.getExpert_face(), vh.sdvImg, UILUtils.getOptions2());
        vh.tvName.setText(expert.getExpert_name());
        vh.tvTitle.setText(expert.getExpert_title());
        vh.tvCatalog.setText("鉴宝类型：" + expert.getAppraisal_catalog());
        vh.tvGood.setText("好评：" + expert.getGood());
        vh.tvTimes.setText("鉴宝次数：" + expert.getTimes());
        vh.tvFee.setText("鉴宝费用：" + expert.getExpert_fee());

        vh.tvSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BaseActivity ba = (BaseActivity) AppManager.getAppManager().currentActivity();
                Bundle bundle = new Bundle();
                bundle.putSerializable("expert", expert);
                ba.jump(UploadJianBaoActivity.class, bundle);
            }
        });
        vh.llTop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BaseActivity ba = (BaseActivity) AppManager.getAppManager().currentActivity();

                Bundle bundle = new Bundle();
                bundle.putInt("type", WebType.EXPERT_INTRO.getId());
                bundle.putInt("expert_id", expert.getId());
                ba.jump(WebActivity.class, bundle);
            }
        });

        LinearLayout.LayoutParams laParams = (LinearLayout.LayoutParams) vh.sdvImg.getLayoutParams();
        DisplayMetrics dm = TDevice.getDisplayMetrics();
        laParams.height = (int) ((dm.widthPixels -
                TDevice.dpToPixel(AppContext.getResource().getDimension(R.dimen.global_padding)) * 2) / 2);
        laParams.width = laParams.height;
        vh.sdvImg.setLayoutParams(laParams);

        return convertView;
    }
}
